﻿<?php
session_start( );
?>
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
require_once('connections/mysqlDB.php');
require("class.phpmailer.php"); 

$dbConn = new mysqli(HOST,USER,PWD,DB);
$dbConn->query("SET NAMES 'utf8'");

$regid=$_SESSION['regid'];
$rid=$_SESSION['rid'];
//echo "regid = $regid";
//echo "rid = $rid";

$stmt1 = $dbConn->query("select cname,first_name,last_name,email
from registrant where regid = $regid");
$data=array();
while ($row = $stmt1->fetch_assoc()) {
$cname= $row['cname'];
$first_name= $row['first_name'];
$last_name= $row['last_name'];
$registrant_email= $row['email'];
};
$stmt1->close();

// get registration data

$stmt1 = $dbConn->query("select cname,CONCAT(trim(first_name),' ',trim(last_name)) as fullname
,pastoral_staff,gender,
(select description from code where abbrev = relationship) as relationship,
(select description from code where abbrev = groups) as groups,
(select description from code where abbrev = special_need) as special_need,
meal1,meal2,meal3,meal4,meal5,meal6,meal7,meal8,session1,session2,
session3,session4,session5,session6,session7,session7,session8,session9,
session10,session11 
from registration_info where rid= $rid");
$data=array();
while ($row = $stmt1->fetch_assoc()) {
$data[]=$row;
};
$stmt1->close();

$confirm = "\nConfirmation ID: $rid\n";

foreach ($data as $c) {
 foreach ($c as $key=>$values) {
  if ( ($key == 'cname') or ($key == 'fullname') or 
  ($key == 'gender') or ($key == 'relationship') or ($key == 'groups')
  or ($key == 'special_need')
  ) {
   //echo "$key...$values<br>";
   $name = $name . $values . "\t";
   }
}
$name = $name . "\n";
}
$title = "\ncname\tName\tGender\tRelationship\tGroup\t\t\tSpecial Need\n";
//echo "$confirm$title\n$name";
$message = $confirm . $title . $name . "\n\nMeals Requested\n";

$stmt1 = $dbConn->query("SELECT d.cname, CONCAT(trim(d.first_name),' ',trim(d.last_name)) as 'Name', (meal1) as '7/2 D' ,(meal2) as '7/3 L' ,
(meal3) as '7/3 D', (meal4) as '7/4 L', (meal5) as '7/4 D',
(meal6) as '7/5 L', (meal7) as '7/5 D' ,(meal8) as '7/6 L'
FROM registrant a, registration b, conference c, registration_info d
WHERE c.conference_id='2009Summer' and a.regid = b.regid
AND b.conference_id = c.conference_id
and b.rid = d.rid  and d.rid=$rid order by d.reginfoid");
$data=array();
while ($row = $stmt1->fetch_assoc()) {
$data[]=$row;
};
$stmt1->close();

$meal="";
$t="";
$n=0;

foreach ($data as $c) {
 $n=$n+1;
 foreach ($c as $key=>$values) {

   //echo "$key...$values<br>";
   
   if ($n == 1) {$t=$t . $key . "\t";}
   $meal = $meal . $values . "\t";
}

$meal = $meal . "\n";
}
$t = $t . "\n";

//echo "\n$t\n$meal";

$message = $message . $t . $meal;
//echo $message;


// send confirmation email

$mail = new PHPMailer();   
  
$mail->IsSMTP();  // telling the class to use SMTP   
$mail->Host     = "smtp.frccinnerlife.org"; // SMTP server  

$mail->Port="587"; 
//$mail->Header = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
$mail->CharSet = "UTF-8";
//$mail->IsHTML(true);  
$mail->Helo = "Support";
$mail->SMTPAuth = true;
$mail->Username = "support@frccinnerlife.org";
$mail->Password = "126255";  

$mail->From     = "FRCC_Admin";   
$mail->AddAddress($registrant_email);   

$msg =  "Dear $first_name $last_name,\n\n Thank you for registering for 2009 Summer Conference with Forerunner Church, 
please confirm the following information.\n\n"; 
 
$mail->Subject  = "2009 Summer Conference Registration";   
$mail->Body     = $msg . $message;  
$mail->WordWrap = 80;   
  
if(!$mail->Send()) {   
if ($stmt = $dbConn->prepare("UPDATE registration SET email_send = NULL
      WHERE rid = ?")) {
    $stmt->bind_param('s', $rid);
    $stmt->execute(); 
    //printf ("Updated records: %d<br>", $stmt->affected_rows);
  }
  echo 'Mailer error: ' . $mail->ErrorInfo;   
} else {   
if ($stmt = $dbConn->prepare("UPDATE registration SET email_send = NOW()
      WHERE rid = ?")) {
    $stmt->bind_param('s', $rid);
    $stmt->execute(); 
    //printf ("Updated records: %d<br>", $stmt->affected_rows);
  }
  echo "Message has been sent to $first_name $last_name @ $registrant_email.";   
}  

?>
</body>
</html>
